package com.inspector.app.ui.flight.statusBar

import android.content.Context
import android.graphics.Typeface
import android.text.Spannable
import android.text.SpannableString
import android.text.Spanned
import android.text.style.AbsoluteSizeSpan
import android.text.style.StyleSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter4.BaseQuickAdapter
import com.inspector.app.databinding.FlightStatusbarBinding
import com.inspector.app.ui.flight.statusBar.FlightStatusBarAdapter.ViewHolder


/**
 * 飞行状态栏
 */
class FlightStatusBarAdapter : BaseQuickAdapter<FlightStatusBar, ViewHolder>() {

    override fun onBindViewHolder(holder: ViewHolder, position: Int, item: FlightStatusBar?) {
        item?.let {
            holder.bind(it)
        }
    }

    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder = ViewHolder(parent)


    class ViewHolder(
        private val parent: ViewGroup,
        private val binding: FlightStatusbarBinding = FlightStatusbarBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        ),
    ) : RecyclerView.ViewHolder(binding.root) {

        fun bind(item: FlightStatusBar) {
            binding.itemTVTitle.text = item.title

            if (item == FlightStatusBar.DQJWD) { //[经纬度单独显示]
                if (item.content.isNullOrEmpty() ||  item.extendContent.isNullOrEmpty()) {
                    binding.itemTVTitle.visibility = View.VISIBLE
                    binding.itemTVContent.visibility = View.VISIBLE
                    binding.itemTVSingleContent.visibility = View.GONE
                    binding.itemTVContent.text = item.content
                    return
                }
                binding.itemTVTitle.visibility = View.GONE
                binding.itemTVContent.visibility = View.GONE
                binding.itemTVSingleContent.visibility = View.VISIBLE
                binding.itemTVSingleContent.text = FlightStatusBarUtils.formatDQJWDSpan(item.content, item.extendContent)
                return
            }

            binding.itemTVTitle.visibility = View.VISIBLE
            binding.itemTVContent.visibility = View.VISIBLE
            binding.itemTVSingleContent.visibility = View.GONE

            if (item == FlightStatusBar.WXS) { //[卫星数]
                binding.itemTVContent.text = FlightStatusBarUtils.formatWXSSpan(item.content, item.extendContent)
                return
            }


            val totalContent = item.content + " " + item.extendContent
            val string = SpannableString(totalContent)
            string.setSpan(StyleSpan(Typeface.BOLD), 0, item.content.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            string.setSpan(StyleSpan(Typeface.NORMAL), item.content.length, totalContent.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            string.setSpan(AbsoluteSizeSpan(16, true), 0, item.content.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            string.setSpan(AbsoluteSizeSpan(12, true), item.content.length, totalContent.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            binding.itemTVContent.text = string
        }

    }

}